﻿using Model;
using Model.Modules;
using Proxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ViewModel
{
    public class VM_WindowConfig:BaseViewModel
    {
        public static surfacedetectEntities surfaceEntities;
        public defect_type AddedDefectType { get { return addedDefectType; } set { addedDefectType = value; OnPropertyChanged(); } }
        private defect_type addedDefectType;

        
        public VM_WindowConfig()
        {
            initData();
            
        }
        void initData() {
            surfaceEntities= new surfacedetectEntities();
            initAddedDefectType();
        }

        public BaseCommand AddDefectCommand { get => new BaseCommand(AddDefectCommand_Executed); }
        public void AddDefectCommand_Executed(object obj)
        {
            AddedDefectType.name = AddedDefectType.name.Trim();
            if (string.IsNullOrEmpty(AddedDefectType.name))
                MessageBox("缺陷名称不能为空");
            if (DefectTypes.Where(t=>t.name== AddedDefectType.name).Count() > 0)
                MessageBox("缺陷名称不能重复，请检查！");
            if (DefectTypes.Where(t => t.color == AddedDefectType.color).Count() > 0)
                MessageBox("缺陷颜色不能重复，请检查！");

            MessageBox("确认增加缺陷", $"是否确认增加缺陷名称为：“{AddedDefectType.name}”的缺陷，增加后将无法删除", new Action<bool>((confirm) => { 
                if (confirm)
                {
                    AddedDefectType.id = DefectTypes.Max(t => t.id) + 1;
                    surfaceEntities.defect_type.Add(AddedDefectType);
                    if (surfaceEntities.SaveChanges() == 1)
                        MessageBox("添加成功");
                    else
                        MessageBox("添加失败");
                    DefectTypes = new System.Collections.ObjectModel.ObservableCollection<defect_type>(surfaceEntities.defect_type.ToList());
                    initAddedDefectType();

                }
            }));
        }
        public void initAddedDefectType() {
            AddedDefectType = new defect_type() { name = "", color = "#FF0000", addbyuser = true, isdetectout = true };
        }
    }
}
